﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Collections.ObjectModel;
using Barbar.MapReport.Common;

namespace Barbar.MapReport.DataSource {
  [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Naming", "CA1710:IdentifiersShouldHaveCorrectSuffix")]
  public class Polygon : ReadOnlyCollection<Polyline>, IGeoBound {
    private GeoBound m_GeoBound;

    public Polygon(IList<Polyline> list)
      : base(list) {
    }

    private void EnsureGeoBound() {
      if (m_GeoBound == null) {
        m_GeoBound = GeoBound.ComputeSharedGeoBound<Polyline>(this);
      }
    }
    
    #region IGeoBound Members

    public IGeoPoint Northeast {
      get {
        EnsureGeoBound();
        return m_GeoBound.Northeast;
      }
    }


    public IGeoPoint Southwest {
      get {
        EnsureGeoBound();
        return m_GeoBound.Southwest;
      }
    }

    #endregion

    public bool ContainsPoint(IGeoPoint point) {
      GeoBound geoBound = new GeoBound(this);
      if (!geoBound.ContainsPoint(point))
        return false;
      foreach (Polyline polyline in this)
        if (polyline.ContainsPoint(point))
          return true;

      return false;
    }
  }
}
